home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / Filezilla Server / FileZilla_Server-0_9_41.exe / source / interface / misc / Led.cpp < prev    next >
C/C++ Source or Header  |  2011-11-06  |  7KB  |  248 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Led.cpp : implementation file
  3. // Visual Source Safe: $Revision: 1.3 $
  4. //
  5. // Led static control. Will display a LED in 4 different colors and two shapes.
  6. ///////////////////////////////////////////////////////////////////////////////
  7. // Copyright (C) 1998-1999 Monte Variakojis ( monte@apollocom.com )
  8. // All rights reserved - not to be sold.
  9. ///////////////////////////////////////////////////////////////////////////////
  10.  
  11. #include "stdafx.h"
  12. #include "../resource.h"
  13. #include "Led.h"
  14.  
  15. #if defined(_DEBUG) && !defined(MMGR)
  16. #define new DEBUG_NEW
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20.  
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CLed
  23. #define TIMER_ID_PING        1        // Timer Ping ID
  24.  
  25. ///////////////////////////////////////////////////////////////////////////////
  26. ///////////////////////////////////////////////////////////////////////////////
  27. CLed::CLed()
  28. {
  29.     m_LedBitmap.LoadBitmap(IDB_LEDS);
  30.     m_nLedColor = LED_COLOR_RED;
  31.     m_nLedMode = LED_OFF;
  32.     m_nLedShape = LED_ROUND;
  33.     m_continuePing = false;
  34. }
  35.  
  36. CLed::~CLed()
  37. {
  38.     VERIFY(m_LedBitmap.DeleteObject());
  39. }
  40.  
  41.  
  42. BEGIN_MESSAGE_MAP(CLed, CStatic)
  43.     //{{AFX_MSG_MAP(CLed)
  44.     ON_WM_PAINT()
  45.     ON_WM_TIMER()
  46.     ON_WM_CREATE()
  47.     ON_WM_ERASEBKGND()
  48.     //}}AFX_MSG_MAP
  49. END_MESSAGE_MAP()
  50.  
  51.  
  52. /////////////////////////////////////////////////////////////////////////////
  53. // CLed message handlers
  54.  
  55. ///////////////////////////////////////////////////////////////////////////////
  56. ///////////////////////////////////////////////////////////////////////////////
  57. void CLed::OnPaint() 
  58. {
  59.     CPaintDC dc(this); // device context for painting
  60.     DrawLed(&dc,m_nLedColor,m_nLedMode,m_nLedShape);
  61.     
  62.     // Do not call CStatic::OnPaint() for painting messages
  63. }
  64.  
  65.  
  66. ///////////////////////////////////////////////////////////////////////////////
  67. // Name:        SetLed
  68. // Description:    This method will draw the LED to the specified DC.
  69. //
  70. // Entry:
  71. //                CDC *pDC - DC to draw to
  72. //
  73. //                int iLedColor - Where color is defined by:
  74. //                     LED_COLOR_RED
  75. //                    LED_COLOR_GREEN
  76. //                    LED_COLOR_YELLOW
  77. //                    LED_COLOR_BLUE
  78. //
  79. //                int iMode - where mode is defined by:
  80. //                    LED_ON
  81. //                    LED_OFF
  82. //                    LED_DISABLED
  83. //
  84. //                int iShape - where shape is defined by:
  85. //                    LED_ROUND
  86. //                    LED_SQUARE
  87. ///////////////////////////////////////////////////////////////////////////////
  88. void CLed::DrawLed(CDC *pDC,int nLEDColor, int nMode, int nShape)
  89. {
  90.     CRect rect;
  91.     GetClientRect(&rect);
  92.  
  93.     //
  94.     // Center led within an oversized window
  95.     //
  96.     if(rect.Width() >= LED_SIZE && rect.Height() >= LED_SIZE)
  97.     {
  98.         int nWidth = rect.Width();
  99.         int nHeight = rect.Height();
  100.         rect.left += (nWidth - LED_SIZE)/2;
  101.         rect.right -= (nWidth - LED_SIZE)/2;
  102.         rect.top += (nHeight - LED_SIZE)/2;
  103.         rect.bottom -= (nHeight - LED_SIZE)/2;
  104.     }
  105.  
  106.     //
  107.     // Prepare temporary DCs and bitmaps
  108.     //
  109.     CBitmap TransBitmap;
  110.     TransBitmap.CreateBitmap(LED_SIZE,LED_SIZE,1,1,NULL);
  111.     CBitmap bitmapTemp;
  112.     CBitmap* pBitmap = &m_LedBitmap;
  113.     CDC srcDC;
  114.     CDC dcMask;
  115.     CDC TempDC;
  116.     TempDC.CreateCompatibleDC(pDC);
  117.     srcDC.CreateCompatibleDC(pDC);
  118.     dcMask.CreateCompatibleDC(pDC);
  119.  
  120.     CBitmap* pOldBitmap = srcDC.SelectObject(pBitmap);
  121.     CBitmap* pOldMaskbitmap = dcMask.SelectObject(&TransBitmap);
  122.     bitmapTemp.CreateCompatibleBitmap(pDC,LED_SIZE,LED_SIZE);
  123.  
  124.     //
  125.     // Work with tempDC and bitmapTemp to reduce flickering
  126.     //
  127.     CBitmap *pOldBitmapTemp = TempDC.SelectObject(&bitmapTemp);
  128.     TempDC.BitBlt(0, 0, LED_SIZE, LED_SIZE, pDC, rect.left, rect.top, SRCCOPY); 
  129.  
  130.     //
  131.     // Create mask
  132.     //
  133.     COLORREF OldBkColor = srcDC.SetBkColor(RGB(255,0,255));
  134.     dcMask.BitBlt(0, 0, LED_SIZE, LED_SIZE,&srcDC, nMode*LED_SIZE, nLEDColor+nShape, SRCCOPY); 
  135.     TempDC.SetBkColor(OldBkColor);
  136.  
  137.     //
  138.     // Using the IDB_LEDS bitmap, index into the bitmap for the appropriate
  139.     // LED. By using the mask color (RGB(255,0,255)) a mask has been created
  140.     // so the bitmap will appear transparent.
  141.     //
  142.     TempDC.BitBlt(0, 0, LED_SIZE, LED_SIZE, &srcDC, nMode*LED_SIZE, nLEDColor+nShape, SRCINVERT); 
  143.     TempDC.BitBlt(0, 0, LED_SIZE, LED_SIZE,&dcMask, 0, 0, SRCAND); 
  144.     TempDC.BitBlt(0, 0, LED_SIZE, LED_SIZE, &srcDC, nMode*LED_SIZE, nLEDColor+nShape, SRCINVERT); 
  145.  
  146.     //
  147.     // Since the actual minipulation is done to tempDC so there is minimal
  148.     // flicker, it is now time to draw the result to the screen.
  149.     //
  150.     pDC->BitBlt(rect.left, rect.top, LED_SIZE, LED_SIZE, &TempDC, 0, 0, SRCCOPY); 
  151.     
  152.     //
  153.     // House cleaning
  154.     //
  155.     srcDC.SelectObject(pOldBitmap);
  156.     dcMask.SelectObject(pOldMaskbitmap);
  157.     TempDC.SelectObject(pOldBitmapTemp);
  158.     VERIFY(srcDC.DeleteDC());
  159.     VERIFY(dcMask.DeleteDC());
  160.     VERIFY(TempDC.DeleteDC());
  161.     VERIFY(TransBitmap.DeleteObject());
  162.     VERIFY(bitmapTemp.DeleteObject());
  163. }
  164.  
  165. ///////////////////////////////////////////////////////////////////////////////
  166. // Name:        SetLed
  167. // Description:    This method will draw and set led parameters.
  168. //
  169. // Entry:        int iLedColor - Where color is defined by:
  170. //                     LED_COLOR_RED
  171. //                    LED_COLOR_GREEN
  172. //                    LED_COLOR_YELLOW
  173. //                    LED_COLOR_BLUE
  174. //
  175. //                int iMode - where mode is defined by:
  176. //                    LED_ON
  177. //                    LED_OFF
  178. //                    LED_DISABLED
  179. //
  180. //                int iShape - where shape is defined by:
  181. //                    LED_ROUND
  182. //                    LED_SQUARE
  183. ///////////////////////////////////////////////////////////////////////////////
  184. void CLed::SetLed(int nLedColor, int nMode, int nShape)
  185. {
  186.     m_nLedColor = nLedColor;
  187.     m_nLedMode = nMode;
  188.     m_nLedShape = nShape;
  189.  
  190.     CDC *pDC;
  191.     pDC = GetDC();
  192.     DrawLed(pDC, m_nLedColor, m_nLedMode, m_nLedShape);
  193.     ReleaseDC(pDC);
  194.     m_bPingEnabled = FALSE;
  195. }
  196.  
  197. ///////////////////////////////////////////////////////////////////////////////
  198. // Name:        Ping
  199. // Description:    This method will turn the led on for dwTimeout milliseconds and
  200. //                then turn it off.
  201. //
  202. // Entry:        DWORD dwTimeout - Time out in  milliseconds
  203. ///////////////////////////////////////////////////////////////////////////////
  204. void CLed::Ping(DWORD dwTimeout)
  205. {
  206.     // Return if pinging
  207.     if (m_bPingEnabled == TRUE)
  208.     {
  209.         m_continuePing = true;
  210.     }
  211.     else
  212.     {
  213.         m_bPingEnabled = TRUE;
  214.         SetLed(m_nLedColor, CLed::LED_ON, m_nLedShape);
  215.         SetTimer(TIMER_ID_PING, dwTimeout, NULL);
  216.     }
  217. }
  218.  
  219. ///////////////////////////////////////////////////////////////////////////////
  220. ///////////////////////////////////////////////////////////////////////////////
  221. void CLed::OnTimer(UINT_PTR nIDEvent) 
  222. {
  223.     if (nIDEvent == TIMER_ID_PING)
  224.     {
  225.         if (m_continuePing)
  226.             m_continuePing = false;
  227.         else
  228.         {
  229.             SetLed(m_nLedColor, CLed::LED_OFF, m_nLedShape);
  230.             KillTimer(nIDEvent);
  231.             m_bPingEnabled = FALSE;
  232.         }
  233.     }
  234.     
  235.     CStatic::OnTimer(nIDEvent);
  236. }
  237.  
  238. ///////////////////////////////////////////////////////////////////////////////
  239. ///////////////////////////////////////////////////////////////////////////////
  240. BOOL CLed::OnEraseBkgnd(CDC* pDC) 
  241. {
  242.     // No background rendering
  243.     return TRUE;
  244. }
  245.  
  246. ///////////////////////////////////////////////////////////////////////////////
  247. ///////////////////////////////////////////////////////////////////////////////
  248.